# coding=utf-8
"""
Copyright (c) Huawei Technologies Co., Ltd. 2020-2028. All rights reserved.
Description:
FileName：search.py
Author: h00568282/huangbin 
Create Date: 2025/9/20 17:09
Notes:

在mcp center中查找类似的mcp server
"""
import os

from sys_intd.exceptions import MCPServerSearchError
from sys_intd.manager import McpServerManager

mcp_server_manager = McpServerManager()

def search(server_name: str, excel: bool = False):
    output_lines = []
    try:
        server_name = server_name.replace("-", "_")
        servers_path = mcp_server_manager.get_mcp_center_servers_path()

        searched_mcp_servers = []
        output_lines.append(f"[bold green]Searching in MCP Center for mcp server '{server_name}'\n[/]")
        for mcp_center_server_name in os.listdir(servers_path):
            if server_name in mcp_center_server_name or mcp_center_server_name in server_name:
                output_lines.append(f"[cyan]{mcp_center_server_name}[/]")
                searched_mcp_servers.append(mcp_center_server_name)

        if not searched_mcp_servers:
            output_lines.append("[yellow]No matched MCP servers in MCP Center.[/]")
            return output_lines
        else:
            output_lines.append(f"\n[bold green]Found {len(searched_mcp_servers)} mcp servers in MCP Center.\n")
    except Exception as e:
        raise MCPServerSearchError(f"Error searching for servers '{server_name}'")

    return output_lines
